package Server.ServerCore.Model.Player;


import Server.ServerCore.Model.AI.BankerAI;
import Server.ServerCore.Service.service.PlayerManagerService;
import Server.ServerCore.Service.syns.Table;
import Server.ServerCore.Service.syns.TableManager;
import Server.ServerCore.Service.hardCode.TableStatus;
import Server.ServerCore.Service.usyns.SingelTable;
import Server.ServerCore.Service.usyns.SingelTableManager;
import org.jboss.netty.buffer.ChannelBuffer;


public abstract class GameCharacter{

    private int ID;//对象编号

    private int CircleUnitID;// 玩家所在CircleID
    private int tableID;//玩家所在牌桌编号

    /**
     * 及时数据发送通道
     *             如果要求及时的数据（譬如地图同步信息）---调用此方法，会直接wirte(buf),
     *             被直接递交给netty通信底层;
     * @param bufferData
     */
    public abstract void DirectSend(ChannelBuffer bufferData);


    /**
     * 对象进入Table  这里ThreadLocal是否同步呢???
     */
    public void IntoTable() {
        Table table=null;
        if (!(this instanceof BankerAI)){
            //匹配table， int[0]:tableID,int[1]:CircleUnitID
            int[] IDs= TableManager.getFreeTable();
            if (IDs!=null){
                table=TableManager.tableFastList.get(IDs[0]);
                table.getGameCharacterFastList().add(this);
                this.setTableID(IDs[0]);//player牌桌编号
                this.setCircleUnitID(IDs[1]);//player牌桌位置
                table.setTableStatus(TableStatus.INTO_TABLE);
                table.logicHandle(this,IDs[1]);

            }
        }else {//BankerAI
            table=TableManager.tableFastList.get(this.getTableID());
            table.setTableStatus(TableStatus.INTO_TABLE);
            table.logicHandle(this,this.getCircleUnitID());
        }
    }


    /**
     * 对象离开Table
     */
    public void LeaveTable() {
        Table table= TableManager.tableFastList.get(this.getTableID());
        table.setTableStatus(TableStatus.LEAVE_TABLE);
        table.logicHandle(this,this.getCircleUnitID());
    }


    /***
     * 获取当前玩家所占有的table，并标记circleUnit false
     */
    public void LeaveSimgleTable(){
        SingelTable singelTable= SingelTableManager.tableFastList.get(this.getTableID());
        for (SingelTable.CircleUnit circleUnit:singelTable.getCircleUnitFastList()){
            circleUnit.setOccupancy(false);
        }
        PlayerManagerService.userOnLine.remove(this.getID());
        PlayerInstance player=(PlayerInstance) this;
        if (!player.isOver()&&player.isBet()){
            PlayerManagerService.DisasterRecovery(player);
        }
        PlayerManagerService.clear(player);

    }

    /**
     * 同步游戏数据
     *
     * @param gameCharacter 活动对象
     * @param buffer
     */
    public void submitSynMsg(GameCharacter gameCharacter, ChannelBuffer buffer) {
        TableManager.tableFastList.get(this.getTableID()).getCircleUnitFastList().get(this.getCircleUnitID()).SendSynMsg(gameCharacter, buffer);
    }



    public int getCircleUnitID() {
        return CircleUnitID;
    }

    public void setCircleUnitID(int circleUnitID) {
        CircleUnitID = circleUnitID;
    }
    public int getTableID() {
        return tableID;
    }

    public void setTableID(int tableID) {
        this.tableID = tableID;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

}
